package monitor

import (
	"html/template"
	"net/http"
	"time"
)
import (
	"r_log"
	"util"
)

/*
处理统计页面的
*/

type StatisticInfo struct {
	ExecTime         time.Time
	DetailStatistics []DetailStatistic
}

type DetailStatistic struct {
	Period      util.Period
	GetStock    int
	NotGetStock int
}

func (s *StatisticInfo) init() {
	s.ExecTime = time.Now()
	rr := monitorunit.Recommender
	s.DetailStatistics = make([]DetailStatistic, util.PERIOD_HHOUR+1)
	for p := util.PERIOD_DAY; p <= util.PERIOD_HHOUR; p++ {
		s.DetailStatistics[p] = DetailStatistic{Period: p, GetStock: rr.Statistic[p].CountOfGetStock, NotGetStock: rr.Statistic[p].CountOfNotGetStock}
	}
}

func (s *StatisticInfo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	s.init() // 初始化数据
	t := template.Must(template.ParseFiles(monitorunit.MonitorConf.StatisticAddr))
	err := t.Execute(w, s)
	if err != nil {
		r_log.Logger.Warn("StateInfo template execute is faild: %v", err)
	}
}
